package com.weiaibenpao.demo.chislim.ui.activity.moment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.tools.DoubleUtils;
import com.luck.picture.lib.widget.PreviewViewPager;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.EventBusBean;
import com.weiaibenpao.demo.chislim.utils.Constant;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class LookHugePhotoActivity extends BaseActivity implements View.OnClickListener{
    public static final int TYPE_MOMENT =0;
    @BindView(R.id.preview_pager)
    PreviewViewPager previewPager;
    @BindView(R.id.mid_text)
    TextView midText;
    @BindView(R.id.right_text)
    TextView rightText;
    /**
     * type==1:发布说说查看大图
     * type==0:跑圈查看大图
     */
    private int type;
    private int currentPosition;
    private List<String> urlList;

    /**
     * set preview image
     *
     * @param position
     * @param medias
     */
    public static void externalPicturePreview(Context context, int position, List<String> medias, int type) {
        if (!DoubleUtils.isFastDoubleClick()) {
            Intent intent = new Intent(context, LookHugePhotoActivity.class);
            intent.putExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST, (Serializable) medias);
            intent.putExtra(PictureConfig.EXTRA_POSITION, position);
            intent.putExtra(Constant.TYPE, type);
            context.startActivity(intent);
            ((Activity) context).overridePendingTransition( R.anim.a5,0);


        }
    }

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_look_huge_photo;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        type = getIntent().getIntExtra(Constant.TYPE, 0);
        currentPosition = getIntent().getIntExtra(PictureConfig.EXTRA_POSITION, 0);
        urlList = ((List<String>) getIntent().getSerializableExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST));
        midText.setText((currentPosition+1)+"/"+urlList.size());
        if (type==1){
            rightText.setVisibility(View.VISIBLE);
        }

        initAdapter();
    }

    private void initAdapter() {

        previewPager.setAdapter(new SimpleFragmentAdapter());
        previewPager.setCurrentItem(currentPosition);
        previewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                midText.setText((position+1)+"/"+urlList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {


            }
        });
    }


    @OnClick({R.id.left_image,R.id.right_text})
    public void onViewClicked(View  view) {
        switch (view.getId()){
            case R.id.left_image:
                finish();
                break;
            case R.id.right_text:
                currentPosition=previewPager.getCurrentItem();
                EventBus.getDefault().post(new EventBusBean(currentPosition));
                //刷新adapter
                notifyList();
                break;
        }
    }
    private void notifyList() {
        if (urlList.size()==1){
            finish();
            return;
        }
        urlList.remove(currentPosition);
        previewPager.setAdapter(null);
        previewPager.setAdapter(new SimpleFragmentAdapter());
        if (currentPosition!=0){
            previewPager.setCurrentItem(currentPosition-1);
        }
        midText.setText((previewPager.getCurrentItem()+1)+"/"+urlList.size());

    }

    @Override
    public void onClick(View v) {

            finish();
            overridePendingTransition(0, R.anim.anim3);
    }

    public class SimpleFragmentAdapter extends PagerAdapter {
        public SimpleFragmentAdapter() {
        }

        public int getCount() {
            return urlList.size();
        }

        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }

        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(LookHugePhotoActivity.this);
            String path = urlList.get(position);

            RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(LookHugePhotoActivity.this).  asBitmap().load(path).apply(options)
                    .into(imageView);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                    overridePendingTransition(0, R.anim.anim3);
                }
            });
            container.addView(imageView);
            return imageView;
        }
    }
}
